	<?php

// 4/11/2010: immanuel192 write this action to save the verse

if (!defined('EXPONENT')) exit('');

global $db;

$verse = null; $verse_id=-1;
// get song id
$song = null; $song_id=-1;
if (isset($_REQUEST['id'])) {
	$_REQUEST['id']=intval($_REQUEST['id']); $song_id = $_REQUEST['id'];
	// get song and collection object
	$sql="SELECT {$db->prefix}present_songs.*, {$db->prefix}present_song_collection.name collection_name, {$db->prefix}present_song_collection.id collection_id
		  FROM {$db->prefix}present_songs INNER JOIN {$db->prefix}present_song_collection
			   ON {$db->prefix}present_songs.collection_id = {$db->prefix}present_song_collection.id
		  WHERE {$db->prefix}present_songs.id = {$song_id}
		  LIMIT 0,1";
	$song = $db->selectObjectsBySql($sql);
	// get first object
	if ($song)
		$song=$song[0];
}

if (!$song)
	validator::failAndReturnToForm('Wrong song value.', $_POST);

// reupdate song infomation
$new_song_name=$_REQUEST['song_name'];
$new_collection_id=intval($_REQUEST['collection_id']);
$new_song_no=intval($_REQUEST['song_no']);

$new_song=null; $new_song->id=$song->id;
$new_song->name=$new_song_name; $new_song->collection_id = $new_collection_id; $new_song->song_no=$new_song_no;

$db->updateObject($new_song,'present_songs');


if (isset($_POST['verse_id'])) {
	$_POST['verse_id']=intval($_POST['verse_id']);
	$verse_id = $_POST['verse_id'];
	
	$verse = $db->selectObject('present_song_verse','id='.$verse_id);
} else {
	$verse->rank = $db->max('present_song_verse', 'rank', 'song_id', "song_id  = ".$song_id);
	if ($verse->rank == null) {
		$verse->rank = 0;
	} else {
		$verse->rank += 1;
	}
}

if (($verse && exponent_permissions_check('edit',$loc)) || (!$verse && exponent_permissions_check('create',$loc))) {
	$verse = slide_song_verse::update($_POST,$verse);
	
	if (($verse->content == "") || ($verse->content == "<p>&#160;</p>"))
		exponent_flow_redirect();
	if (isset($verse->id)) {
		$db->updateObject($verse,'present_song_verse');
	} else {
		$db->insertObject($verse,'present_song_verse');
	}
	exponent_flow_redirect();
} else {
	echo SITE_404_HTML;
}

?>
